#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${GREEN}Installing dependencies for CTPQuoteEngine...${NC}"

# 检测包管理器
if command -v apt-get &> /dev/null; then
    PKG_MANAGER="apt-get"
    INSTALL_CMD="apt-get install -y"
    UPDATE_CMD="apt-get update"
elif command -v yum &> /dev/null; then
    PKG_MANAGER="yum"
    INSTALL_CMD="yum install -y"
    UPDATE_CMD="yum update"
else
    echo -e "${RED}Error: No supported package manager found${NC}"
    exit 1
fi

# 检查是否有root权限
if [ "$EUID" -ne 0 ]; then
    echo -e "${YELLOW}Please run as root${NC}"
    exit 1
fi

# 更新包列表
echo -e "${GREEN}Updating package lists...${NC}"
$UPDATE_CMD

# 安装基本开发工具
echo -e "${GREEN}Installing basic development tools...${NC}"
if [ "$PKG_MANAGER" = "apt-get" ]; then
    $INSTALL_CMD build-essential git cmake
else
    $INSTALL_CMD gcc gcc-c++ make git cmake
fi

# 安装Qt5
echo -e "${GREEN}Installing Qt5...${NC}"
if [ "$PKG_MANAGER" = "apt-get" ]; then
    $INSTALL_CMD qt5-default qtbase5-dev qttools5-dev-tools
else
    $INSTALL_CMD qt5-qtbase-devel qt5-qttools-devel
fi

# 安装spdlog
echo -e "${GREEN}Installing spdlog...${NC}"
if [ "$PKG_MANAGER" = "apt-get" ]; then
    $INSTALL_CMD libspdlog-dev
else
    $INSTALL_CMD spdlog-devel
fi

# 创建必要的目录
echo -e "${GREEN}Creating necessary directories...${NC}"
mkdir -p /home/leexMarketLib/{flow,logs}
chmod 777 /home/leexMarketLib/{flow,logs}

echo -e "${GREEN}Dependencies installation completed!${NC}"
echo -e "${YELLOW}Note: Make sure CTP API libraries are properly installed in include/CTP directory${NC}" 